import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import vuetify from './plugins/vuetify';
import { createToast } from './plugins/toast';
import 'font-awesome/css/font-awesome.min.css';
import axios from 'axios';
import qs from 'qs';
import {
	getCurDate,
	setSessionStorage,
	getSessionStorage,
	removeSessionStorage,
	setLocalStorage,
	getLocalStorage,
	removeLocalStorage
} from './common.js';

// 创建 Vue 应用实例
const app = createApp(App);

// 设置 axios 的基础 URL
axios.defaults.baseURL = 'http://localhost:8888/elm/';

// 将 axios 和 qs 挂载到全局属性上
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;

// 创建并挂载Toast实例
const toastInstance = createToast();
app.config.globalProperties.$toast = toastInstance;

// 将常用的工具函数挂载到全局属性上
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;

// 全局路由守卫
router.beforeEach((to, from, next) => {
	const user = sessionStorage.getItem('user');
	// 判断是否需要登录，排除登录、注册、首页、商家列表和商家信息页面
	if (
        !(to.path === '/' || to.path === '/index' || to.path === '/businessList' || 
        to.path === '/businessInfo' || to.path === '/login' || to.path === '/register')
	) {
		if (!user) {
			// 使用 Vue Router 的重定向机制
			next('/login');
			return;
		}
	}
	next();
});

// 挂载路由和Vuetify并渲染应用
app.use(router);
app.use(vuetify);
app.mount('#app');
